#!/bin/sh
clear
echo 先判断是否使用root权限运行
#==================================================================================
 if [ $(whoami) = "root" ];then echo " 温馨提示: 请不要使用root身份运行";exit 0;fi
 
echo 再判断~/是否存在Socket的目录
#++++++++++++++++++++ 2 获取当前系统的桌面名称和当前系统密码++++++++++++++++++++++++++       
if [ ! -d $HOME/Socket ];then echo " 温馨提示: 请先将对应的Socket放到主目录先!";exit 0;fi

#++++++++++++++++++++ 2 获取当前系统的桌面名称和当前系统密码++++++++++++++++++++++++++
read -p "请输入当前系统密码:" SysPassword
if [ ! -d $HOME/Desktop ];then DesktopPath=$HOME"/桌面";else DesktopPath=$HOME"/Desktop";fi && clear

echo 获取当前浏览器的启动命令
read -p "请输入当前WEB浏览器的启动命令(例如:火狐firefox-esr %u   UOS浏览器/usr/bin/uos-browser-stable %U):" WEBCON
#WEBCON='firefox-esr %u'
echo 初始化路径和变量
#==================================================================================
AutoExec=$HOME"/.config/autostart"	#启动项的目录是:$AutoExec
ApplicationsPath="/usr/share/applications"	#公共快捷方式目录是:$ApplicationsPath
AppPath=$HOME"/Socket"	#Socket目录是:$AppPath
VideoIco="ico-vs"	#VideoServer图标路径:$VideoIco
AppIco="ico-if"	#"无纸化后台图标路径:$AppIco
VideoServerName="VideoServer"	#VideoServer.desktop
WebServerName="英飞无纸化会议管理系统"	#英飞无纸化会议管理系统.html
EXE="start.sh"	#要启动的文件:$EXE
NetworkCardName=$(ifconfig | grep 'enp' | awk -F ":" '{print $1}' | sed 's/,/\n\r/g')
temp_path="/mnt/lv"



#==================================================================================

echo '$SysPassword' | sudo -S chmod -R 777 $AppPath && clear

echo 挂载并安装网络中的永中office
#==================================================================================
if [ ! -d $temp_path ];then sudo mkdir $temp_path && sudo chmod -R 777 $temp_path;fi
#判断临时挂载目录中的永中office是否存在,不存在则需要挂载共享文件夹
if [ ! -f $temp_path/1.yozo-office_8.0.1331.101ZH.S1_amd64.deb ]; then
    sudo mount -t cifs -o username=administrator,password=123qwe //192.168.1.186/XunFei $temp_path	#挂载网络中指定IP的共享文件夹"XunFei"到本地的"$temp_path"
    sleep 2	#延时2秒后,再次判断永中安装包是否存在(判断临时目录是否挂载成功,不成功则退出)
    if [ ! -f $temp_path/1.yozo-office_8.0.1331.101ZH.S1_amd64.deb ]; then
        echo $temp_path"/1.yozo-office_8.0.1331.101ZH.S1_amd64.deb 文件不存在,10秒后退出脚本,请排除错误后,再次运行我吧"
        sleep 10
        exit 0
    fi
fi

echo 安装永中office和todesk远程协助工具
#==================================================================================
sudo dpkg -i $temp_path/1.yozo-office_8.0.1331.101ZH.S1_amd64.deb  #安装永中office,无纸化后台服务器必须安装的

echo 安装远程工具[todesk]
#==================================================================================
if [ ! -f $DesktopPath/todesk_4.1.0_amd64.deb ];then wget -P $DesktopPath https://dl.todesk.com/linux/todesk_4.1.0_amd64.deb && sudo chmod 777 $DesktopPath/todesk_4.1.0_amd64.deb && sudo dpkg -i $DesktopPath/todesk_4.1.0_amd64.deb;fi

echo 创建APP执行脚本[start.sh]
#==================================================================================
if [ -f {AppPath}/$EXE ];then mv $AppPath/$EXE $AppPath/bak-$EXE && rm -rf $AppPath/$EXE;fi
touch $AppPath/$EXE && sudo chmod 777 $AppPath/$EXE
echo '#!/bin/sh'> $AppPath/$EXE
echo "echo '$SysPassword' | sudo -S chmod 777 /dev/ttyU*" >> $AppPath/$EXE
echo 'pkill java' >> $AppPath/$EXE
echo 'pkill MachineControl' >> $AppPath/$EXE
echo "cd $AppPath/MachineControl-linux-x64">> $AppPath/$EXE
echo 'nohup ./MachineControl -v 0403 -m 6001 -r 2400 -p 5506 -u ff101100dd -d ff101100ee >./Uart.log 2>&1 &'>> $AppPath/$EXE
echo 'sleep 1' >> $AppPath/$EXE
echo 'cd ..' >> $AppPath/$EXE
echo "java -Xmx8g -Xms4g -jar MyJavaServer_v2.1.4_rt.jar -eth $NetworkCardName -mac ./maclist -web 4567 >./nohup.out 2>&1 &" >> $AppPath/$EXE


echo 创建:VidoeServer.desktop快捷方式
#==================================================================================
if [ -f $ApplicationsPath/$VideoServerName.desktop ];then sudo rm -rf $ApplicationsPath/$VideoServerName.desktop;fi 	#删除$ApplicationsPath/$VideoServerName的快捷方式
sudo touch $ApplicationsPath/$VideoServerName.desktop
sudo chmod 777 $ApplicationsPath/$VideoServerName.desktop
echo '[Desktop Entry]'> $ApplicationsPath/$VideoServerName.desktop
echo "Name=$VideoServerName">> $ApplicationsPath/$VideoServerName.desktop
echo 'Comment=Socket'>> $ApplicationsPath/$VideoServerName.desktop
echo "Exec=$AppPath/$EXE %u">> $ApplicationsPath/$VideoServerName.desktop
echo "Icon=$AppPath/$AppIco">> $ApplicationsPath/$VideoServerName.desktop
echo 'Terminal=true'>> $ApplicationsPath/$VideoServerName.desktop
echo 'Type=Application'>> $ApplicationsPath/$VideoServerName.desktop
echo 'Categories=Application;Development;'>> $ApplicationsPath/$VideoServerName.desktop
if [ -L $AutoExec/$VideoServerName.desktop ] ;then  rm -rf $AutoExec/$VideoServerName.desktop;else ln -s $ApplicationsPath/$$VideoServerName.desktop $AutoExec/$VideoServerName.desktop;fi
if [ -L $DesktopPath/$VideoServerName.desktop ] ;then  rm -rf $DesktopPath/$VideoServerName.desktop;else ln -s $ApplicationsPath/$$VideoServerName.desktop $DesktopPath/$VideoServerName.desktop;fi


echo 创建 WEB后台管理[英飞无纸化会议管理系统.desktop]的快捷方式
#==================================================================================
if [ -f $ApplicationsPath/$WebServerName.desktop ];then sudo rm -rf $ApplicationsPath/$WebServerName.desktop;fi 	#删除$ApplicationsPath/$WebServerName.desktop
sudo touch $ApplicationsPath/$WebServerName.desktop
sudo chmod 777 $ApplicationsPath/$WebServerName.desktop
echo '[Desktop Entry]'> $ApplicationsPath/$WebServerName.desktop
echo "Name=$WebServerName">> $ApplicationsPath/$WebServerName.desktop
echo 'Comment=Socket'>> $ApplicationsPath/$WebServerName.desktop
echo "Exec=$WEBCON 127.0.0.1/admin">> $ApplicationsPath/$WebServerName.desktop
echo "Icon=$AppPath/$AppIco">> $ApplicationsPath/$WebServerName.desktop
echo 'Terminal=false'>> $ApplicationsPath/$WebServerName.desktop
echo 'Type=Application'>> $ApplicationsPath/$WebServerName.desktop
echo 'Categories=Application;Development;'>> $ApplicationsPath/$WebServerName.desktop
if [ -L $DesktopPath/$WebServerName.desktop ] ;then  rm -rf $DesktopPath/$WebServerName.desktop;else ln -s $ApplicationsPath/$WebServerName.desktop $DesktopPath/$WebServerName.desktop;fi
echo '==============================='

echo 获取本机信息
#==================================================================================
rm -rf $DesktopPath/本机配置.txt
touch $DesktopPath/本机配置.txt
echo | lscpu >$DesktopPath/本机配置.txt && echo "==============================================">>$DesktopPath/本机配置.txt
echo | uname -a >>$DesktopPath/本机配置.txt && echo "==============================================">>$DesktopPath/本机配置.txt
echo | cat /etc/os-release >>$DesktopPath/本机配置.txt && echo "==============================================">>$DesktopPath/本机配置.txt

#cat $AppPath/$EXE
echo "本机活动网卡名称是： "$NetworkCardName
sleep 3